#!/bin/bash

# 检查参数是否正确
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <port>"
    exit 1
fi

PORT=$1

# 使用 lsof 查找占用端口的进程
PID=$(lsof -t -i :$PORT)

# 检查是否找到了进程
if [ -z "$PID" ]; then
    echo "No process found listening on port $PORT"
    exit 0
fi

# 终止进程
echo "Killing process $PID listening on port $PORT"
kill -9 $PID

# 检查是否成功终止进程
if [ $? -eq 0 ]; then
    echo "Process $PID has been killed"
else
    echo "Failed to kill process $PID"
    exit 1
fi